﻿using DLL.NET50.DB.MS.ORM.UOW;

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using DLL.NET50.DB.CRUD;
using DLL.NET50.DB.MS.Entity;
using DLL.NET60.DB.MS.Entity.DTO;
using Org.BouncyCastle.Crypto;
using AutoMapper;
using DLL.Standard.Infrastructure.Log;

namespace DLL.NET50.DB.MS.ORM.Repository
{
    public interface IWorkFileRepository : IBaseRepository<adi_work_file>
    {
        List<DTO_adi_work_file> GetAllWorkFile();

        List<DTO_adi_work_file> GetAllWorkFile(int erkeMode);

        bool AddWorkFile(DTO_adi_work_file entity);

        bool SaveWorkFile(DTO_adi_work_file entity);

        bool DeleteWorkFile(DTO_adi_work_file entity);

        bool UpdateWorkFile(DTO_adi_work_file entity);

        List<DTO_adi_work_file> WfWorkFile(string drugCode, string pycode, string drugname);
    }

    public class CWorkFileRepository : BaseRepository<adi_work_file>, IWorkFileRepository
    {
        private IMapper _mapper;

        public CWorkFileRepository(IMSUnitOfWork unitOfWork, IMapper mapper) : base(unitOfWork.Db)
        {
            _mapper = mapper;
        }

        public List<DTO_adi_work_file> GetAllWorkFile()
        {
            try
            {
                var dic = base.GetAll(p => p.DRUG_PYCODE);
                List<DTO_adi_work_file> dTOs = _mapper.Map<List<DTO_adi_work_file>>(dic.ToList());
                return dTOs;
            }
            catch (Exception ex)
            {
                LogHelper.Warn("执行获取药品字典信息出现异常：" + ex.Message);
            }
            return null;
        }

        public List<DTO_adi_work_file> GetAllWorkFile(int erkeMode)
        {
            try
            {
                var dic = Where<string>(c => c.ErKeMode == erkeMode, o => o.DRUG_PYCODE);
                List<DTO_adi_work_file> dTOs = _mapper.Map<List<DTO_adi_work_file>>(dic.ToList());
                return dTOs;
            }
            catch (Exception ex)
            {
                LogHelper.Warn("执行获取药品字典信息出现异常：" + ex.Message);
            }
            return null;
        }

        //添加
        public bool AddWorkFile(DTO_adi_work_file entityDTO)
        {
            try
            {
                adi_work_file entity = _mapper.Map<adi_work_file>(entityDTO);
                base.Add(entity);
            }
            catch (Exception ex)
            {
                LogHelper.Warn("执行插入药品字典出现异常：" + ex.Message);
                return false;
            }
            return true;
        }

        //删除
        public bool DeleteWorkFile(DTO_adi_work_file entityDTO)
        {
            try
            {
                adi_work_file entity = _mapper.Map<adi_work_file>(entityDTO);
                base.Remove(entity);
            }
            catch (Exception ex)
            {
                LogHelper.Warn("执行删除药品字典信息出现异常：" + ex.Message);
                return false;
            }
            return true;
        }

        //修改
        public bool UpdateWorkFile(DTO_adi_work_file entityDTO)
        {
            try
            {
                adi_work_file entity = _mapper.Map<adi_work_file>(entityDTO);
                base.Update(entity);
            }
            catch (Exception ex)
            {
                LogHelper.Warn("执行更新药品字典出现异常：" + ex.Message);
                return false;
            }
            return true;
        }

        public bool SaveWorkFile(DTO_adi_work_file entityDTO)
        {
            try
            {
                adi_work_file entity = _mapper.Map<adi_work_file>(entityDTO);
                string drugCode = entity.FILE_NAME;
                var info = Where(c => c.FILE_NAME == drugCode);
                List<adi_work_file> ld = info.ToList();
                if (ld.Count == 0)
                {
                    entity.ID = 0;
                    //不存在，在添加
                    Add(entity);
                }
                else if (ld.Count == 1)
                {
                    //存在，则修改
                    Update(entity);
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Warn("保存加工文件出现异常：" + ex.Message);
                return false;
            }

            return true;
        }

        //配药文件信息查询
        /*
            * s => s.XianWID.StartsWith(str)
           匹配以str开头的
           s => s.XianWID.EndsWith(str)
           匹配以str结尾的
           s => s.XianWID.Contains(str)
           匹配包含str的
            */

        public List<DTO_adi_work_file> WfWorkFile(string drugCode, string pycode, string drugname)
        {
            try
            {
                //1.Linq to Entities
                Expression<Func<adi_work_file, bool>> where = c => c.DRUG_CODE == drugCode
                            || c.DRUG_PYCODE == pycode
                            || c.DRUG_NAME == drugname
                            || c.DRUG_CODE.Contains(drugCode)
                            || c.DRUG_NAME.Contains(drugname)
                            || c.DRUG_PYCODE.Contains(pycode);

                var dic = Where<string>(where, o => o.DRUG_NAME);
                List<DTO_adi_work_file> dTOs = _mapper.Map<List<DTO_adi_work_file>>(dic.ToList());
                return dTOs;
            }
            catch (Exception ex)
            {
                LogHelper.Warn("执行查询加工文件出现异常出现异常：" + ex.Message);
            }
            return null;
        }
    }
}